package com.Web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/Hello")
public class HelloServlet extends HttpServlet {
    public HelloServlet(){
        System.out.println("HelloServlet instance");
    }

    @Override
    public void init(){
        System.out.println("HelloServlet init");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
        System.out.println("HelloServlet service");
    }

    @Override
    public void destroy() {
        super.destroy();
        System.out.println("HelloServlet destory");
    }
    //处理GET请求
    @Override
    protected  void  doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        //设置请求编码
        req.setCharacterEncoding("UTF-8");
        //获得请求参数
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        System.out.println("name:"+name+"age:"+age);
        resp.getWriter().write("Hello!!!Servlet!!!");
        //给浏览器返回一行数据
        resp.getWriter().write("Hello!!姓名是：" + name + "年龄是：" + age);
        //重定向到百度
        resp.sendRedirect("http://www.baidu.com");
    }
}
